package com.usefullc.crawler.component;

import com.usefullc.common.constants.SystemConstant;
import com.usefullc.common.exception.BizException;
import com.usefullc.system.service.AbstractService;
import com.usefullc.system.service.interceptor.AspectProcess;
import com.usefullc.user.domain.User;
import com.usefullc.user.enums.UserLevelEnum;
import com.usefullc.user.service.UserService;
import com.usefullc.user.service.component.UserManager;
import com.usefullc.user.vo.UserVo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Sampson
 * @date: 2022-05-13 18:00
 */
@Component
public class CrawlerAspectProcess extends AbstractService implements AspectProcess {
    private List<String> urls = new ArrayList<>();
    {
        urls.add("/crawlerClient/task/exportData");
        urls.add("/crawler/activeProcess/start");

    }

    @Autowired
    private UserService userService;

    @Override
    public void process(ProceedingJoinPoint joinPoint, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        //判断用户是否过期，过期用户某些功能不能使用1
        UserVo loginUser = UserManager.getLoginUser();
        if(loginUser != null && urls.contains(requestURI)){
            User user = userService.findById(loginUser.getId());
            if(user.getLevel() == UserLevelEnum.NORMAL.getCode() && user.getGmtExpire().getTime() < System.currentTimeMillis()){
                throw new BizException(SystemConstant.ResponseCode.VIP_EXPIRE, "使用权限已过期");
            }
        }
    }

    @Override
    public void clean() {

    }
}
